﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection; // For Missing.Value and BindingFlags 
using System.Runtime.InteropServices; // For COMException 
using System.Diagnostics;
using System.IO;
using System.Globalization;

namespace VietKinsNutrition.Common
{
    public class ExcelFunc
    {
        public static void KillExcelProcess()
        {
            //*************************************************************** 
            //Get all instances of proc that are open, attempt to close them. 
            //*************************************************************** 
            try
            {
                //*********************************************************** 
                // Loop through all the processes and kill ones of that type 
                //*********************************************************** 
                foreach (Process thisproc in Process.GetProcessesByName("excel"))
                {
                    if (!thisproc.CloseMainWindow())
                    {
                        //*************************************************** 
                        // If closing is not successful or no desktop window 
                        // handle, then force termination. 
                        //*************************************************** 
                        thisproc.Kill();
                    }
                } // next proc 

            }
            catch (Exception Exc)
            {
                Exc.Source = "";
            }
        }

        public static string NextChar(string Asc)
        {
            if (Asc == "A") return "B";
            if (Asc == "B") return "C";
            if (Asc == "C") return "D";
            if (Asc == "D") return "E";
            if (Asc == "E") return "F";
            if (Asc == "F") return "G";
            if (Asc == "G") return "H";
            if (Asc == "H") return "I";
            if (Asc == "I") return "J";
            if (Asc == "J") return "K";
            if (Asc == "K") return "L";
            if (Asc == "L") return "M";
            if (Asc == "M") return "N";
            if (Asc == "N") return "O";
            if (Asc == "O") return "P";
            if (Asc == "P") return "Q";
            if (Asc == "Q") return "R";
            if (Asc == "R") return "S";
            if (Asc == "S") return "T";
            if (Asc == "T") return "U";
            if (Asc == "U") return "V";
            if (Asc == "V") return "W";
            if (Asc == "W") return "X";
            if (Asc == "X") return "Y";
            if (Asc == "Y") return "Z";
            if (Asc == "Z") return "AA";
            if (Asc == "AA") return "AB";
            if (Asc == "AB") return "AC";
            if (Asc == "AC") return "AD";
            if (Asc == "AD") return "AE";
            if (Asc == "AE") return "AF";
            if (Asc == "AF") return "AG";
            if (Asc == "AG") return "AH";
            if (Asc == "AH") return "AI";
            if (Asc == "AI") return "AJ";
            if (Asc == "AJ") return "AK";
            if (Asc == "AK") return "AL";
            if (Asc == "AL") return "AM";
            if (Asc == "AM") return "AN";
            if (Asc == "AN") return "AO";
            if (Asc == "AO") return "AP";
            if (Asc == "AP") return "AQ";
            if (Asc == "AQ") return "AR";
            if (Asc == "AR") return "AS";
            if (Asc == "AS") return "AT";
            if (Asc == "AT") return "AU";
            if (Asc == "AU") return "AV";
            if (Asc == "AV") return "AW";
            if (Asc == "AW") return "AX";
            if (Asc == "AX") return "AY";
            if (Asc == "AY") return "AZ";
            if (Asc == "AZ") return "BA";
            if (Asc == "BA") return "BB";
            if (Asc == "BB") return "BC";
            if (Asc == "BC") return "BD";
            if (Asc == "BD") return "BE";
            if (Asc == "BE") return "BF";
            if (Asc == "BF") return "BG";
            if (Asc == "BG") return "BH";
            if (Asc == "BH") return "BI";
            if (Asc == "BI") return "BJ";
            if (Asc == "BJ") return "BK";
            if (Asc == "BK") return "BL";
            if (Asc == "BL") return "BM";
            if (Asc == "BM") return "BN";
            if (Asc == "BN") return "BO";
            if (Asc == "BO") return "BP";
            if (Asc == "BP") return "BQ";
            if (Asc == "BQ") return "BR";
            if (Asc == "BR") return "BS";
            if (Asc == "BS") return "BT";
            if (Asc == "BT") return "BU";
            if (Asc == "BU") return "BV";
            if (Asc == "BV") return "BW";
            if (Asc == "BW") return "BX";
            if (Asc == "BX") return "BY";
            if (Asc == "BY") return "BZ";
            if (Asc == "BZ") return "CA";
            if (Asc == "CA") return "CB";
            if (Asc == "CB") return "CC";
            if (Asc == "CC") return "CD";
            if (Asc == "CD") return "CE";
            if (Asc == "CE") return "CF";
            if (Asc == "CF") return "CG";
            if (Asc == "CG") return "CH";
            if (Asc == "CH") return "CI";
            if (Asc == "CI") return "CJ";
            if (Asc == "CJ") return "CK";
            if (Asc == "CK") return "CL";
            if (Asc == "CL") return "CM";


            return "";
        }

        public static string NextChar(string Asc, int step)
        {
            if (step == 0)
                return Asc;
            Asc = NextChar(Asc);
            return NextChar(Asc, step - 1);
        }

        public static string PrevChar(string Asc)
        {
            if (Asc == "B") return "A";
            if (Asc == "C") return "B";
            if (Asc == "D") return "C";
            if (Asc == "E") return "D";
            if (Asc == "F") return "E";
            if (Asc == "G") return "F";
            if (Asc == "H") return "G";
            if (Asc == "I") return "H";
            if (Asc == "J") return "I";
            if (Asc == "K") return "J";
            if (Asc == "L") return "K";
            if (Asc == "M") return "L";
            if (Asc == "N") return "M";
            if (Asc == "O") return "N";
            if (Asc == "P") return "O";
            if (Asc == "Q") return "P";
            if (Asc == "R") return "Q";
            if (Asc == "S") return "R";
            if (Asc == "T") return "S";
            if (Asc == "U") return "T";
            if (Asc == "V") return "U";
            if (Asc == "W") return "V";
            if (Asc == "X") return "W";
            if (Asc == "Y") return "X";
            if (Asc == "Z") return "Y";
            return "";
        }

        public static string NextExcelCol(string Asc)
        {
            if (Asc.Length == 1) return NextChar(Asc);
            if (Asc.Length == 2)
            {
                string First = Asc[0].ToString();
                string Last = Asc[1].ToString();
                if (Last == "Z")
                {
                    return NextChar(First) + "A";
                }
                else
                {
                    return First + NextChar(Last);
                }
            }
            return "ERROR";
        }


        public static string PrevExcelCol(string Asc)
        {
            if (Asc.Length == 1) return PrevChar(Asc);
            if (Asc.Length == 2)
            {
                if (Asc == "AA") return "Z";

                string First = Asc[0].ToString();
                string Last = Asc[1].ToString();
                if (Last == "A")
                {
                    return PrevChar(First) + "Z";
                }
                else
                {
                    return First + PrevChar(Last);
                }
            }
            return "ERROR";
        }
        public static string SaveResouceFile(string FileName)
        {
            string FilePath = System.IO.Path.GetTempPath() + System.Guid.NewGuid().ToString() + "_" + FileName;
            Assembly Assemb = Assembly.GetExecutingAssembly();
            Stream stream = Assemb.GetManifestResourceStream("VietKins.Resources." + FileName);

            FileStream fs = new FileStream(FilePath, FileMode.Create);
            BinaryReader br = new BinaryReader(stream);
            byte[] save = new byte[stream.Length];
            br.Read(save, 0, save.Length);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(save, 0, save.Length);
            bw.Flush();
            bw.Close();
            return FilePath;
        }
    }
}
